package com.cetccst.securitysuite.uicomponents.watermark;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

import java.text.SimpleDateFormat;

public class WaterMarkTextView extends TextView {

    private String[] labels;
    private float degrees = -30;
    private float spaceY = 200;
    private float spaceX = 200;
    private int textColor = 0x0A000000;
    private float textSize = sp2px(15f);
    private boolean showTime;
    private BroadcastReceiver timeReceiver;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public WaterMarkTextView(Context context) {
        super(context);
    }

    public WaterMarkTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (!showTime && (labels == null || labels.length <= 0)) {
            return;
        }

        String time = null;

        int width = getWidth();
        int height = getHeight();

        Paint paint = new Paint();
        paint.setColor(textColor);

        paint.setAntiAlias(true);
        paint.setTextSize(textSize);
        canvas.save();
        canvas.rotate(degrees);
        float textWidth = 0;
        for (String str : labels) {
            float textWidthTemp = paint.measureText(str);
            if (textWidthTemp > textWidth) {
                textWidth = textWidthTemp;
            }
        }

        if (showTime) {
            time = dateFormat.format(System.currentTimeMillis());
            if (paint.measureText(time) > textWidth) {
                textWidth = paint.measureText(time);
            }
        }

        float textSizeHeight = paint.getTextSize();
        float textHeight = textSizeHeight * (labels.length + (showTime ? 1 : 0));

        int index = 0;
        for (float positionY = 0; positionY <= height + spaceY; positionY += textHeight + spaceY) {
            float fromX = index++ % 2 * textWidth - (Math.abs(index++ % 2 * textWidth) % width + 1) * width;
            for (float positionX = fromX; positionX < width + spaceX; positionX += textWidth + spaceX) {
                float spacing = 0;//间距
                for (String label : labels) {
                    canvas.drawText(label, positionX, positionY + spacing, paint);
                    spacing = spacing + textSizeHeight;
                }
                if (showTime) {
                    canvas.drawText(time, positionX, positionY + spacing, paint);
                }
            }
        }

        canvas.restore();
    }

    @Override
    protected void onDetachedFromWindow() {
        if (timeReceiver != null) {
            getContext().unregisterReceiver(timeReceiver);
            timeReceiver = null;
        }
        super.onDetachedFromWindow();
    }

    public void setWaterMarkText(String[] labels) {
        this.labels = labels;
    }

    protected int sp2px(float sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }

    public void setShowTime(boolean showTime) {
        this.showTime = showTime;
        if (showTime) {
            if (timeReceiver == null) {
                timeReceiver = new TimeReceiver();
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction(Intent.ACTION_TIME_TICK);
                getContext().registerReceiver(timeReceiver, intentFilter);
            }
        } else {
            if (timeReceiver != null) {
                getContext().unregisterReceiver(timeReceiver);
                timeReceiver = null;
            }
        }
    }

    class TimeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_TIME_TICK.equals(intent.getAction())) {
                //刷新draw方法
                postInvalidate();
            }
        }
    }
}